package city.spring.modules.system.service.impl;

import city.spring.modules.system.entity.PositionsEntity;
import city.spring.modules.system.repository.PositionsRepository;
import city.spring.modules.system.service.PositionsService;
import city.spring.modules.system.vo.PositionsEntityVO;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service：岗位
 *
 * @author pxm
 * @date 2020-08-06 15:48:00
 */
@CacheConfig(cacheNames = {PositionsServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Exception.class)
@Service
public class PositionsServiceImpl extends ServiceImpl<PositionsRepository, PositionsEntity> implements PositionsService {

    private final PositionsRepository positionsRepository;

    public PositionsServiceImpl(PositionsRepository positionsRepository) {
        this.positionsRepository = positionsRepository;
    }

    @Override
    public void savePositions(PositionsEntity entity) {
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updatePositions(PositionsEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deletePositions(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deletePositions(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public IPage<PositionsEntityVO> listPage(Page<? extends PositionsEntity> page, Wrapper<? extends PositionsEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }

    @Override
    public List<PositionsEntity> getPharmacist() {
        return positionsRepository.getPharmacist();
    }

    @Override
    public List<PositionsEntity> getDoctor() {
        return positionsRepository.getDoctor();
    }
}